package freemind.controller;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import freemind.controller.PanelSelectListener;
import freemind.main.FreeMindApplet;
import freemind.view.mindmapview.NodeView;

public class ChangeTextPanel extends JPanel{
	public final static int MODE_SELECT = 1;
	public final static int MODE_ADD = 2;
	public final static int MODE_EDIT = 3;
	public final static int MODE_REMOVE = 4;
	
	public final static int COM_INSERIR = 1;
	public final static int COM_ALTERAR = 2;
	public final static int COM_EXCLUIR = 4;
	public final static int COM_FORMATAR = 8;
	public final static int COM_LINK = 16;
	public final static int COM_LINK_REM = 32;
	public final static int COM_POSICAO = 64;
	public final static int COM_COPIA = 128;
	public final static int COM_PASTE = 256;
	
	private FreeMindApplet appletInstance;
	
	/*
	 * 	import java.awt.*;
		import java.applet.*;
		// These classes are for Url's.
		import java.net.*;
		
		 URL base = applet.getDocumentBase()
		 my_gif = getImage(base,"imageExample.gif");
		 
		 http://www.realapplets.com/tutorial/ImageExample.html
	 */
	
	private JPanel pnEdit;
	
		private JScrollPane scrollPane;
		private JTextArea txtTextNode;
		private JLabel lblChange;
		private JLabel lblCancel;
		
	private JPanel pnSelect;
	
		private JLabel lblAdd;
		private JLabel lblEdit;
		private JLabel lblRemove;
		private JLabel lblPosition;
		private JLabel lblLayout;
		private JLabel lblLink;
		private JLabel lblRemoverLink;
		private JLabel lblCopia;
		private JLabel lblPaste;
		
		private Image imgAdd;
		private Image imgAddOver;
		
		private Image imgEdit;
		private Image imgEditOver;
		
		private Image imgRemove;
		private Image imgRemoveOver;
		
		private Image imgPosition;
		private Image imgPositionOver;
		
		private Image imgLayout;
		private Image imgLayoutOver;		
		
		private Image imgOk;
		private Image imgOkOver;
		
		private Image imgCancel;
		private Image imgCancelOver;
		
		private Image imgLink;
		private Image imgLinkOver;		
		
		private Image imgRemoverLink;
		private Image imgRemoverLinkOver;
		
		private Image imgCopia;
		private Image imgCopiaOver;
		
		private Image imgPaste;
		private Image imgPasteOver;		
		
	private PanelSelectListener listenerPanel;
	
	private static ChangeTextPanel instance;
	private int modeSelected;	
	private NodeView node;
	
	private boolean mouseOverPanel,mouseOverLabel;
	private boolean panelBlocked = false;
	
	
	private ChangeTextPanel(NodeView node,FreeMindApplet appletInstance){
		this.node = node;
		this.modeSelected = MODE_SELECT;
		
		this.appletInstance = appletInstance;
		
		loadImages();
		initComponnents();
		layoutSettings();
	}
	
	private void loadImages(){
		URL path;		
		
		try {
			path = FreeMindApplet.getBaseURL();
			
			imgAdd = (new ImageIcon( appletInstance.getResource("images/add.jpg") )).getImage();
			
			imgAddOver = (new ImageIcon( appletInstance.getResource("images/addOver.jpg") )).getImage();
			
			imgRemove = (new ImageIcon( appletInstance.getResource("images/remove.jpg") )).getImage();
			
			imgRemoveOver = (new ImageIcon( appletInstance.getResource("images/removeOver.jpg") )).getImage();					
			
			imgEdit = (new ImageIcon( appletInstance.getResource("images/edit.jpg") )).getImage();
			
			imgEditOver = (new ImageIcon( appletInstance.getResource("images/editOver.jpg") )).getImage();
			
			imgPosition = (new ImageIcon( appletInstance.getResource("images/position.jpg") )).getImage();
			
			imgPositionOver = (new ImageIcon( appletInstance.getResource("images/positionOver.jpg") )).getImage();	
			
			imgLayout = (new ImageIcon( appletInstance.getResource("images/layout.jpg") )).getImage();
			
			imgLayoutOver = (new ImageIcon( appletInstance.getResource("images/layoutOver.jpg") )).getImage();		
			
			imgOk = (new ImageIcon( appletInstance.getResource("images/ok.jpg") )).getImage();
			
			imgOkOver = (new ImageIcon( appletInstance.getResource("images/okOver.jpg") )).getImage();	
		
			
			imgCancel = (new ImageIcon( appletInstance.getResource("images/remove.jpg") )).getImage();
			
			imgCancelOver = (new ImageIcon( appletInstance.getResource("images/removeOver.jpg") )).getImage();
			
			imgLink = (new ImageIcon( appletInstance.getResource("images/link.jpg") )).getImage();
			
			imgLinkOver = (new ImageIcon( appletInstance.getResource("images/linkOver.jpg") )).getImage();
			
			imgRemoverLink = (new ImageIcon( appletInstance.getResource("images/removerLink.jpg") )).getImage();			
			
			imgRemoverLinkOver = (new ImageIcon( appletInstance.getResource("images/removerLinkOver.jpg") )).getImage();		
			
			imgCopia = (new ImageIcon( appletInstance.getResource("images/copia.jpg") )).getImage();
			
			imgCopiaOver = (new ImageIcon( appletInstance.getResource("images/copia-over.jpg") )).getImage();

			imgPaste = (new ImageIcon( appletInstance.getResource("images/paste.jpg") )).getImage();		
			
			imgPasteOver = (new ImageIcon( appletInstance.getResource("images/paste-over.jpg") )).getImage();			
			
			
			imgAdd = Util.deixaTransparente(imgAdd, Color.white);
			imgAddOver = Util.deixaTransparente(imgAddOver, Color.white);
			imgRemove = Util.deixaTransparente(imgRemove, Color.white);			
			imgRemoveOver = Util.deixaTransparente(imgRemoveOver, Color.white);			
			imgEdit = Util.deixaTransparente(imgEdit,Color.white);			
			imgEditOver = Util.deixaTransparente(imgEditOver,Color.white);			
			imgPosition = Util.deixaTransparente(imgPosition,Color.white);			
			imgPositionOver = Util.deixaTransparente(imgPositionOver,Color.white);			
			imgLayout = Util.deixaTransparente(imgLayout,Color.white);			
			imgLayoutOver = Util.deixaTransparente(imgLayoutOver,Color.white);			
			imgOk = Util.deixaTransparente(imgOk,Color.white);
			imgOkOver = Util.deixaTransparente(imgOkOver,Color.white);			
			imgCancel = Util.deixaTransparente(imgCancel,Color.white);			
			imgCancelOver = Util.deixaTransparente(imgCancelOver,Color.white);
			imgLink = Util.deixaTransparente(imgLink,Color.white);		
			imgLinkOver = Util.deixaTransparente(imgLinkOver,Color.white);
			imgRemoverLink = Util.deixaTransparente(imgRemoverLink,Color.white);		
			imgRemoverLinkOver = Util.deixaTransparente(imgRemoverLinkOver,Color.white);
			imgCopia = Util.deixaTransparente(imgCopia,Color.white);
			imgCopiaOver = Util.deixaTransparente(imgCopiaOver,Color.white);
			imgPaste = Util.deixaTransparente(imgPaste,Color.white);
			imgPasteOver = Util.deixaTransparente(imgPasteOver,Color.white);			
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void initComponnents(){
		this.setOpaque(false);
		this.addMouseListener(new MousePanelListener() );
			
		pnEdit = new JPanel();
		pnEdit.setBackground(Color.white);
			txtTextNode = new JTextArea();
			txtTextNode.setLineWrap(true);
			txtTextNode.setWrapStyleWord(true);
			
			scrollPane = new JScrollPane(txtTextNode);
			scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			scrollPane.setPreferredSize( new Dimension(200,80) );
			
			lblChange = new JLabel(new ImageIcon(imgOk) );
			lblCancel = new JLabel(new ImageIcon(imgCancel) );
			
			PanelEditListener pnEditListener = new PanelEditListener();
			
			lblChange.addMouseListener(pnEditListener);
			lblCancel.addMouseListener(pnEditListener);
					
			
		pnSelect = new JPanel();
		pnSelect.setBackground(Color.white);		
			lblAdd = new JLabel( new ImageIcon(imgAdd) );
			lblRemove = new JLabel( new ImageIcon(imgRemove) );
			lblEdit = new JLabel( new ImageIcon(imgEdit) );
			lblPosition = new JLabel( new ImageIcon(imgPosition) );
			lblLayout = new JLabel( new ImageIcon(imgLayout) );
			lblLink = new JLabel( new ImageIcon(imgLink));
			lblRemoverLink = new JLabel( new ImageIcon(imgRemoverLink));
			lblCopia= new JLabel( new ImageIcon(imgCopia));
			lblPaste= new JLabel( new ImageIcon(imgPaste));
			
			SelectListener pnSelectListener = new SelectListener();
			
			lblAdd.addMouseListener( pnSelectListener );
			lblRemove.addMouseListener( pnSelectListener );
			lblEdit.addMouseListener( pnSelectListener );
			lblPosition.addMouseListener( pnSelectListener );		
			lblLayout.addMouseListener( pnSelectListener );		
			lblLink.addMouseListener( pnSelectListener );
			lblRemoverLink.addMouseListener( pnSelectListener );
			lblCopia.addMouseListener( pnSelectListener );
			lblPaste.addMouseListener( pnSelectListener );
			
	}
	
	private void layoutSettings(){		
		int interval = 10;
		pnEdit.setLayout( new GridBagLayout() );
		
			GridBagConstraints cnTextNode = new GridBagConstraints();
			GridBagConstraints cnChange = new GridBagConstraints();
			GridBagConstraints cnCancel = new GridBagConstraints();
			
			cnTextNode.gridx = 0;
			cnTextNode.gridy = 0;
			cnTextNode.gridwidth = 3;
			cnTextNode.fill = GridBagConstraints.VERTICAL;
			
			pnEdit.add(scrollPane,cnTextNode);
						
			cnChange.gridwidth = 2;
			cnChange.gridx = 1;
			cnChange.gridy = 1;
			cnChange.anchor = GridBagConstraints.EAST;
			
			pnEdit.add(lblChange,cnChange);
			
			cnCancel.gridx = 3;
			cnCancel.gridy = 1;
			
			pnEdit.add(lblCancel,cnCancel);
			
		pnEdit.setPreferredSize( new Dimension( pnEdit.getPreferredSize().width +  2*interval,
											   pnEdit.getPreferredSize().height )
											 );					
		
		pnSelect.add(lblAdd);
		pnSelect.add(lblEdit);
		pnSelect.add(lblRemove);
		pnSelect.add(lblPosition);
		pnSelect.add(lblLayout);
		pnSelect.add(lblLink);
		pnSelect.add(lblRemoverLink);
		pnSelect.add(lblCopia);
		pnSelect.add(lblPaste);
		
		pnSelect.setPreferredSize( new Dimension( pnSelect.getPreferredSize().width + interval,
				  								  pnSelect.getPreferredSize().height)
								 );			
		
		add(pnSelect);
		add(pnEdit);
		
		setMainPanel();
		
	}
	
	private void setMainPanel(){
		int xPlus;
		
		if( node == null){
			return;
		}
			
		if( this.modeSelected == MODE_SELECT ){
			this.pnEdit.setVisible(false);
			this.pnSelect.setVisible(true);
		}
		else if( this.modeSelected == MODE_EDIT){
			this.pnEdit.setVisible(true);
			this.pnSelect.setVisible(false);
		}		
		
        if( node.isLeft() ){
        	xPlus = - getWidth() - 10;        	
        }
        else{
        	xPlus = node.getWidth() + 10;
        }
        
        setBounds(node.getX() - 25, node.getY() - 30,
        		getPreferredSize().width,
        		getPreferredSize().height);			
		
        
        repaint();
	}
	
	private class MousePanelListener extends MouseAdapter{
		
		public void mouseEntered(MouseEvent e){
			mouseOverPanel = true;		
		}
		
		public void mouseExited(MouseEvent e){
			mouseOverPanel = false;		
		}		
	}
	
	private class SelectListener extends MouseAdapter{
	
		public void mouseEntered(MouseEvent e){
			mouseOverLabel = true;		
			
			if( e.getSource() == lblAdd ){
				lblAdd.setIcon( new ImageIcon(imgAddOver) );
				appletInstance.out("Inserir");
			}
			else if( e.getSource() == lblEdit ){
				lblEdit.setIcon( new ImageIcon(imgEditOver) );
				appletInstance.out("Editar");
			}
			else if( e.getSource() == lblRemove ){
				lblRemove.setIcon( new ImageIcon(imgRemoveOver) );
				appletInstance.out("Remover");
			}		
			else if( e.getSource() == lblPosition ){
				lblPosition.setIcon( new ImageIcon(imgPositionOver) );
				appletInstance.out("Espelho");
			}	
			else if( e.getSource() == lblLayout ){
				lblLayout.setIcon( new ImageIcon(imgLayoutOver) );
				appletInstance.out("Trocar Tipo");
			}			
			else if( e.getSource() == lblLink ){
				lblLink.setIcon( new ImageIcon(imgLinkOver) );
				appletInstance.out("Linkar");
			}
			else if( e.getSource() == lblRemoverLink ){
				lblRemoverLink.setIcon( new ImageIcon(imgRemoverLinkOver) );
				appletInstance.out("Remover Link");
			}										
			else if( e.getSource() == lblCopia ){
				lblCopia.setIcon( new ImageIcon(imgCopiaOver) );
				appletInstance.out("Copiar");
			}									
			else if( e.getSource() == lblPaste ){
				lblPaste.setIcon( new ImageIcon(imgPasteOver) );
				appletInstance.out("Colar");
			}										
				
			
		}
		
		public void mouseExited(MouseEvent e){
			mouseOverLabel = false;
			
			if( lblAdd.equals( e.getSource() ) ){
				lblAdd.setIcon( new ImageIcon(imgAdd) );
			}
			else if( lblEdit.equals( e.getSource() ) ){
				lblEdit.setIcon( new ImageIcon(imgEdit) );
			}
			else if( lblRemove.equals( e.getSource() ) ){
				lblRemove.setIcon( new ImageIcon(imgRemove) );
			}		
			else if( e.getSource() == lblPosition ){
				lblPosition.setIcon( new ImageIcon(imgPosition) );
			}	
			else if( e.getSource() == lblLayout ){
				lblLayout.setIcon( new ImageIcon(imgLayout) );
			}		
			else if( e.getSource() == lblLink ){
				lblLink.setIcon( new ImageIcon(imgLink) );
			}				
			else if( e.getSource() == lblRemoverLink ){
				lblRemoverLink.setIcon( new ImageIcon(imgRemoverLink) );
			}				
			else if( e.getSource() == lblCopia ){
				lblCopia.setIcon( new ImageIcon(imgCopia) );
			}
			else if( e.getSource() == lblPaste ){
				lblPaste.setIcon( new ImageIcon(imgPaste) );
			}										
		
			
		}			
		
		public void mouseClicked(MouseEvent e){
			JLabel lblSource = (JLabel) e.getSource();
			
			if( lblSource.equals(lblAdd) ){
				listenerPanel.AddButtonClick( getInstance() );
				update(MODE_ADD);
				destroy();
			}
			else if( lblSource.equals(lblEdit) ){
				listenerPanel.EditButtonClick( getInstance() );
				editAction();
			}
			else if( lblSource.equals(lblRemove)){
				listenerPanel.RemoveButtonClick( getInstance() );
				update(MODE_REMOVE);				
			}
			else if( lblSource.equals(lblPosition)){
				listenerPanel.PositionButtonClick( getInstance() );
				update(MODE_REMOVE);				
			}
			else if( lblSource.equals(lblLayout)){
				listenerPanel.LayoutButtonClick( getInstance() );
				update(MODE_REMOVE);				
			}
			else if( lblSource.equals(lblLink)){
				listenerPanel.LinkButtonClick( getInstance() );
				update(MODE_REMOVE);				
			}	
			else if( lblSource.equals(lblRemoverLink)){
				listenerPanel.RemoverLinkButtonClick( getInstance() );
				update(MODE_REMOVE);				
			}
			else if( lblSource.equals(lblCopia)){
				listenerPanel.CopiaButtonClick( getInstance() );			
			}
			else if( lblSource.equals(lblPaste)){
				listenerPanel.PasteButtonClick( getInstance() );			
			}						
		}
		
	}
	
	private class PanelEditListener extends MouseAdapter{
		
		public void mouseEntered(MouseEvent e){
			if( lblChange.equals( e.getSource() ) ){
				lblChange.setIcon( new ImageIcon( imgOkOver ) );
			}
			else if( lblCancel.equals( e.getSource() ) ){
				lblCancel.setIcon( new ImageIcon( imgCancelOver ) );
			}
		}
		
		public void mouseExited(MouseEvent e){
			if( lblChange.equals( e.getSource() ) ){
				lblChange.setIcon( new ImageIcon( imgOk ) );
			}
			else if( lblCancel.equals( e.getSource() ) ){
				lblCancel.setIcon( new ImageIcon( imgCancel ) );
			}			
		}	
		
		public void mouseClicked(MouseEvent e){
			if( lblChange.equals( e.getSource() ) ){
				listenerPanel.ChangeButtonClick( getInstance() );
				update(MODE_SELECT);
				destroy();				
			}
			else if( lblCancel.equals( e.getSource() ) ){
				listenerPanel.CancelButtonClick( getInstance() );
				cancelAction();	
			}
					
		}		
				
	}
	
	public String getTextNode(){
		return this.txtTextNode.getText();
	}
	
	public void editAction(){
		showUp();
		updateText();
		update(MODE_EDIT);		
	}
	
	public void cancelAction(){
		if( modeSelected == MODE_EDIT ){
			destroy();
		}
	}
	
	
	private void updateText(){
		this.txtTextNode.setText( node.getModel().toString() );
	}
		
	private void setNodeView(NodeView node){
		this.node = node;
		
		FreeMindApplet ap = (FreeMindApplet) appletInstance;
		int visibleFunctions = ap.getCustomController().getNodeVisibleFunctions(node);
		
		lblPosition.setVisible( (visibleFunctions & COM_POSICAO) > 0);
		lblAdd.setVisible( (visibleFunctions & COM_INSERIR) > 0);
		lblEdit.setVisible( (visibleFunctions & COM_ALTERAR) > 0);
		lblLayout.setVisible( (visibleFunctions & COM_FORMATAR) > 0);
		lblLink.setVisible( (visibleFunctions & COM_LINK) > 0);
		lblRemoverLink.setVisible( (visibleFunctions & COM_LINK_REM) > 0 );
		lblRemove.setVisible( (visibleFunctions & COM_EXCLUIR) > 0);
		lblCopia.setVisible( (visibleFunctions & COM_COPIA) > 0);
		lblPaste.setVisible( (visibleFunctions & COM_PASTE) > 0);
	}
	
	public static ChangeTextPanel getInstance(){
		return instance;
	}
	
	public static ChangeTextPanel getInstance(NodeView node,FreeMindApplet appletInstance){
		if( instance == null ){
			instance = new ChangeTextPanel(node,appletInstance);
		}
		else{
			if( node != null){
				instance.setNodeView(node);
			}
		}
		
		return instance;
	}
	
	public void addPanelSelectListener(PanelSelectListener listener){
		this.listenerPanel = listener;
	}
		
	
	public boolean isToGetNewNode(){
		if( (mouseOverLabel || mouseOverPanel) || modeSelected == MODE_EDIT ){
			return false;
		}
		else{
			return true;			
		}
	}
	
	public NodeView getNode(){
		return this.node;
	}
	
	public void update(int mode){
		this.modeSelected = mode;
		setMainPanel();
	}	
	
	public void showUp(){
		if( !panelBlocked ){
			this.setVisible(true);
			this.update(modeSelected);
		}
	}
	
	public void unshow(){
		if( modeSelected == MODE_EDIT ){
			listenerPanel.ChangeButtonClick(this);
			cancelAction();
		}
		this.setVisible(false);
		this.update(modeSelected);
	}
	
	public void destroy(){
		this.setVisible(false);
		this.modeSelected = MODE_SELECT;
		
		this.mouseOverLabel = false;
		this.mouseOverPanel = false;
	}
		
	public void blockPanel(){
		this.panelBlocked = true;
		this.unshow();
	}
	
	public void freePanel(){
		this.panelBlocked = false;
	}
	
}
